//
//  StringViewController.swift
//  swift各类控件集锦
//
//  Created by iosdev1 on 2017/1/6.
//  Copyright © 2017年 zyy. All rights reserved.
//

import UIKit

class StringViewController: ZyBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

//        swift3.0 中不能直接使用下标数字进行字符串解决，只能使用String.Index来做位置索引，要想实现截取功能首先得获取到String.Index;
        //        汉字转拼音带音调/不带音调
        let string1 = func1(string: "我是中国人")
        print("汉字转拼音带音调/不带音调：\(string1)")
        
//        截取后几位字符
        let string2 = func2(string: "abcdefghijklmnopqrstuvwxyz", x: 2)
        print("截取后2位字符：\(string2)")
        
        let hebing:String = string1 + string2
        print("\(hebing)")
        
        //        获取开头字符几个字符
        let string3 = func3(string: "abcdefghijklmnopqrstuvwxyz", x: 2)
        print("获取开头字符2个字符：\(string3)")
        
//        获取3到5的字符串，包含
        let string4 = func4(string: "abcdefghijklmnopqrstuvwxyz", indexFrom: 3, indexTo: 5)
        print("\(string4)")
        
//        杂乱
        let string5 = func5(string: "abcdefghijklmnopqrstuvwxyz")
        print("\(string5)")
        
//        字符串包含字符的下表（开始下表）
        let string6 = func6(string: "abcdefghijklmnopqrstuvwxyz", containedStr: "fgh")
        print("\(string6)")
        
//        在指定位置插入相应字符串
        let string7 = func7(string: "abcdefghijklmnopqrstuvwxyz", insertStr: "~!@", location: 2)
        print("\(string7)")
        
        //在指定位置删除一个字符
        let string8 = func8(string: "abcdefghijklmnopqrstuvwxyz", dex: 27)
        print("\(string8)")
        
//        用指定的字符替换特定字符
        let string9 = func9(string: "abcdefghijklmanopqrstuvwxyz", replace: "a", with: "123")
        print("\(string9)")
        
        //在指定范围替换一个字符串
        let string10 = func10(string: "abcdefghijklmnopqrstuvwxyz", from: 0, to: 5, replaceS: "123")
        print("\(string10)")
        
//        NSInteger 转字符串  还有其他各种类型的转化
        let string11 = func11(string: 1234567890)//这样子转化没有Optional，如果带有？那就会有Optional
        print("\(string11)")
        let string11s = "sd" + string11
        print("\(string11s)")
        
        
        let ss = NSString.init(string: String(1243223))
        let sdf = (ss as String) + "sssssi"
        print("\(sdf)")
        
        
        //字母大小写
        print(string11.uppercased())
        print(string11.lowercased())
    }

    func func1(string:String) -> String {
        //        汉字转拼音带音调
        let pinyinContain = zyUtilsChineseToStringContainTone(hanzi: string)
        print("汉字转拼音带音调：\(pinyinContain)")
        //        汉字转拼音不带音调
        let pinyinNone = zyUtilsChineseToStringNoneTone(hanzi: string)
        print("汉字转拼音不带音调：\(pinyinNone)")
        return pinyinContain
    }
    func func2(string:String, x:NSInteger) -> String {
        //        获取结尾两个字符子串：
        let indexEnd = string.index(string.endIndex, offsetBy: -x)
        let suffixEnd = string.substring(from: indexEnd)
        
//        let send = NSString(string:string)
//        let suf = send.substring(with: NSMakeRange(send.length-2, 2))
//        print("获取结尾两个字符子串：\(suf)")

        return suffixEnd
    }
    func func3(string:String, x:NSInteger) -> String {
        //        获取开头字符两个：
        let indexHead = string.index(string.startIndex, offsetBy: 2)
        let prefix = string.substring(to: indexHead)
        
//        let shead = NSString(string:"abcdefghijklmnopqrstuvwxyz")
//        let pre = shead.substring(to: 2)
//        print("获取开头字符两个：\(pre)")
        return prefix
    }
    func func4(string:String, indexFrom:NSInteger, indexTo:NSInteger) -> String {
        //        获取中间（3 5）
        //通过范围获取字符串中的一个子串,先截取前八个，在截取后五个,在func5中也有另外一种方法，仅供参考
        let preStr = string.substring(to: string.index(string.startIndex, offsetBy: (indexFrom + indexTo)));
        let subStr = preStr.substring(from: preStr.index(preStr.endIndex, offsetBy: -indexTo))
        
//        let smid=NSString(string:"abcdefghijklmnopqrstuvwxyz")
//        let mids = smid.substring(with: NSMakeRange(3, 5))
//        print("获取中间（3 5）:\(mids)")
        return subStr
    }
    
    func func5(string:String) -> String {
//        字符串长度
        print("字符串长度:\(string.lengthOfBytes(using: String.Encoding.utf8))")
        //获取某个下标后一个下标对应的字符
        let char = string[string.index(after: string.startIndex)]
        print("某个下标后一个:\(char)")
        
        //获取某个下标前一个下标对应的字符
        let char2 = string[string.index(before: string.endIndex)]
        print("某个下标前一个:\(char2)")
        
        //通过范围获取字符串中的一个子串
        let subString = string[string.startIndex...string.index(string.startIndex, offsetBy: 4)]
        let subString2 = string[string.index(string.endIndex, offsetBy: -5)..<string.endIndex]
        print("通过范围获取字符串中的一个子串:\(subString) \n \(subString2)")
        
        //追加字符串操作
        print("\(string.appending("添加字符串"))")
        return string

    }
    func func6(string:String, containedStr:String) -> NSInteger {
        //        获取某个子串在父串中的范围
        let range = string.range(of: containedStr)
        let locationStr:String = String(describing: range?.lowerBound.samePosition(in: string.utf16))
        let endStr = locationStr.substring(from: locationStr.index(locationStr.endIndex, offsetBy: -3))
        let preStr = endStr.substring(to: endStr.index(endStr.startIndex, offsetBy: 1))
        return NSInteger(preStr)!
    }
    func func7(string:String, insertStr:String, location:NSInteger) -> String {
        //在指定位置插入一个字符
        var returnStr = string  //返回这个字符串
        var deleteStr = insertStr  //        用这个字符串提取字符
        
        for i in 0...insertStr.lengthOfBytes(using: String.Encoding.utf8) {
            let insertStr = deleteStr.substring(to: deleteStr.index(deleteStr.startIndex, offsetBy: 1))
            
            let insertChar = Character(insertStr)
            returnStr.insert(insertChar, at: returnStr.index(returnStr.startIndex, offsetBy: location))
            if i != (insertStr.lengthOfBytes(using: String.Encoding.utf8) - 1) {
                deleteStr.remove(at: returnStr.startIndex)
            }
        }
        
//            鉴于Collection 死活都不行，只能另想其他办法
//            var arrs:Collection = ["~","~"];
//            insertStr.insert(contentsOf: Collection, at: <#T##String.Index#>)
//            insertStr.insert(contentsOf: ["s"], at: insertStr.index(insertStr.startIndex, offsetBy: location))
        return returnStr
    }
    
    func func8(string:String, dex:NSInteger) -> String {
        var sMid = string
        //在指定位置删除一个字符
        if sMid.lengthOfBytes(using: String.Encoding.utf8) >= dex {
            if dex == 0 {
                sMid.remove(at: sMid.startIndex)
            }else{
                sMid.remove(at: sMid.index(before: sMid.index(sMid.startIndex, offsetBy: dex)))
            }
        }else{
            zyUIshowMessage(message: "字符串长度不够")
        }
        return sMid
    }

    func func9(string:String, replace:String, with:String) -> String {
        let a=NSString(string:string)
        let b=a.replacingOccurrences(of: replace, with: with)
        return b
    }
    func func10(string:String, from:NSInteger, to:NSInteger, replaceS:Any) -> String {
        var sMid = string
        //在指定范围替换一个字符串
//        sMid.replaceSubrange(sMid.startIndex...sMid.index(sMid.startIndex, offsetBy: 3), with: replaceS as! String)
//        
//        sMid.replaceSubrange(sMid.startIndex...sMid.index(sMid.startIndex, offsetBy: 3), with: ["1","2","3"])
        sMid.replaceSubrange(sMid.index(sMid.startIndex, offsetBy: from)...sMid.index(sMid.startIndex, offsetBy: to), with: replaceS as! String)
        return sMid
    }
    
    func func11(string:NSInteger) -> String {
//        字符串转NSInteger
        let str1="100"
        let intv = (str1 as NSString).intValue
        print("\(intv)")
        let interv = (str1 as NSString).integerValue
        print("\(interv)")
        let l = (str1 as NSString).longLongValue //=100
        print("\(l)")
        
        let str2 = "10.8"
        print("\(str2)")
        let f = (str2 as NSString).floatValue //=10.8
        print("\(f)")
        let d = (str2 as NSString).doubleValue //=10.8
        print("\(d)")
        
        let str3 = "true"
        let b = (str3 as NSString).boolValue //=true
        print("\(b)")
        
        print("\(intv) + \(interv)")
        
        return String(string)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
